1.5. Reviewing the Generated Script
Open the file you created, and
let's take a quick look at the generated T-SQL. The following snippet
from what you see shows the creation of three tables and three stored
procedures, and a few INSERT statements
that add rows of data to some of the tables. Except for the things you
told the script-generation wizard to ignore, the following T-SQL looks
like all other object creation T-SQL you typically deal with on a daily
basis:
/****** Object: Table [dbo].[Users] Script Date: 03/31/2010 23:39:20 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Users](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[NTUserName] [nvarchar](128) NULL,
[Domain] [nvarchar](50) NOT NULL,
[Intro] [nvarchar](100) NULL,
[Title] [nvarchar](50) NOT NULL,
[State] [nvarchar](10) NOT NULL,
[Country] [nvarchar](100) NULL,
[PWD] [varbinary](100) NULL,
[rowguid] [uniqueidentifier] DEFAULT NEWSEQUENTIALID()
PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[Users] ON
INSERT [dbo].[Users] ([ID], [Name], [NTUserName], [Domain], [Intro], [Title],
[State], [Country], [PWD])
VALUES (1, N'Herve Roggero', N'hroggero', N'PYNLOGIC',
N'Enterprise and Azure Architect; Speaker. Expert knowledge in C#. Prev. mgmt exp.',
N'Azure Architect', N'FL', N'US',
0xE8F97FBA9104D1EA5047948E6DFB67FACD9F5B73)
INSERT [dbo].[Users] ([ID], [Name], [NTUserName], [Domain], [Intro], [Title],
[State], [Country], [PWD])
VALUES (2, N'Jim Mullis', N'jmullis', N'PYNLOGIC',
N'Expert in software development. C++; Oracle; SQL Server DBA', N'', N'FL', N'US', 0xE8F97FBA9104D1EA5047948E6DFB67FACD9F5B73)
INSERT [dbo].[Users] ([ID], [Name], [NTUserName], [Domain], [Intro], [Title],
[State], [Country], [PWD])
VALUES (3, N'Scott Klein', N'sklein', N'',
N'Expert in software development. MVP SQL Server. Author. Speaker.',
N'Architect', N'FL', N'US', 0xE8F97FBA9104D1EA5047948E6DFB67FACD9F5B73)
SET IDENTITY_INSERT [dbo].[Users] OFF
/****** Object: StoredProcedure [dbo].[proc_CreateProfile]
Script Date: 03/31/2010 23:39:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[proc_CreateProfile]
@uid [nvarchar](50),
@pwd [nvarchar](50),
@name [nvarchar](50),
@title [nvarchar](50),
@country [nvarchar](50),
@state [nvarchar](20),
@rowguid uniqueidentifier
WITH RECOMPILE, ENCRYPTION
AS
DECLARE @password varbinary(100)
SET @password = HASHBYTES('sha1', @pwd)
-- Make sure the UID is not already taken...
IF (Exists(SELECT TOP 1 * FROM Users WHERE NTUserName = @uid))
BEGIN
RAISERROR(N'0x001 - User ID already in use', 16, 1)
END
ELSE
BEGIN
INSERT INTO Users
(Name, NTUserName, Domain, Intro, Title, State, Country, PWD, rowguid)
VALUES
(@name, @uid, '', '', @title, @state, @country, @password, @rowguid)
END
GO
Notice that the script enables several options, such as ANSI_NULL and ANSI_PADDING.
Then, the script creates the Users table. This table has an IDENTITY
column as well as a rowguid column that uses the uniqueidentifier
database. The rowguid column also has a default on it, which uses the NEWSEQUENTIALID() function to automatically generate new GUIDs. This table is created on the PRIMARY file group, followed by the setting of several table options via the WITH clause.
Further down in the script,
several stored procedures are created, one of which is shown in the
preceding code snippet. proc_CreateProfile is a standard stored
procedure that accepts several input parameters and uses the WITH option to specify procedure options: in this case, RECOMPILE
(to indicate that the database engine doesn't need to cache a plan for
this procedure and to compile the procedure at runtime) and ENCRYPTION (indicating that SQL Server converts the text of this stored procedure to an obfuscated format).